//
//  YSProdSkuView.m
//  Yueshi
//
//  Created by Northwan on 15/7/25.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSProdSkuView.h"
#import "CustomShowView.h"
#define PROD_IMG_HEIGHT 60
#define PROD_IMG_WIDTH 63

#define PROD_NAME_FONT_SIZE 13

#define PROD_NEWPRICE_FONT_SIZE 18
#define PROD_OLDPRICE_FONT_SIZE 15

#define TITLE_FONT_SIZE 15

@interface YSProdSkuView()<SkuSelectViewDelegate>
@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) UIButton *buyBtn;;
@property (strong, nonatomic) UIImageView *prodImage;;
@property (strong, nonatomic) UILabel *nameLabel;;
@property (strong, nonatomic) UILabel *newsPriceLabel;;
@property (strong, nonatomic) UILabel *oldPriceLabel;;
@property (strong, nonatomic) UIButton *closeBtn;
@property (strong, nonatomic) UILabel *standardLabel;
@property (strong, nonatomic) SkuSelectView *skuSelectView;
@property (strong, nonatomic) UILabel *numLabel;
@property (strong, nonatomic) YSProductDetailRes *result;


@end
@implementation YSProdSkuView

-(instancetype)initWithProdDetail:(YSProductDetailRes *)prodDetail{
    _result = prodDetail;
    self = [super init];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
    
        

        //商品图片
        _prodImage = [[UIImageView alloc]initWithFrame:CGRectMake(YSPublicXInset, YSPublicXInset+5, PROD_IMG_WIDTH, PROD_IMG_HEIGHT)];
        [_prodImage setImageURLStr:prodDetail.prodImages[0]];
        [self addSubview:_prodImage];
        
        //商品名字
        _nameLabel = [[UILabel alloc]init];
        CGFloat nameLabelW = SCREENW-YSPublicXInset*7-PROD_IMG_HEIGHT;
        [_nameLabel setNumberOfLines:2];
        [_nameLabel setTextColor:HEXColor(@"#333333")];
        [_nameLabel setFont:[UIFont systemFontOfSize:PROD_NAME_FONT_SIZE]];
        [_nameLabel setFrame:CGRectMake(CGRectGetMaxX(_prodImage.frame)+YSPublicXInset, YSPublicXInset+5, nameLabelW, PROD_NAME_FONT_SIZE*2+YSPublicXInset)];
        [_nameLabel setText:prodDetail.prodName];
        [self addSubview:_nameLabel];
        
        
        
        //现价(如果是抢购活动时，就显示活动价格)
        CGFloat price = 0;
        if(prodDetail.prodPromotion.isPanicBuy){
            price = prodDetail.prodPromotion.price;
        }else{
            price = prodDetail.prodNewPrice;
        }
        CGSize newPriceSize =  [[NSString stringWithFormat:@"¥%.1f",price] sizeWithFont:SYS_FONT(PROD_NEWPRICE_FONT_SIZE)];
        _newsPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(_nameLabel.x, CGRectGetMaxY(_prodImage.frame)-PROD_NEWPRICE_FONT_SIZE,newPriceSize.width,PROD_NEWPRICE_FONT_SIZE)];
        [_newsPriceLabel setTextColor:ThemeColor];
        [_newsPriceLabel setFont:[UIFont systemFontOfSize:PROD_NEWPRICE_FONT_SIZE]];
        [_newsPriceLabel setText:[NSString stringWithFormat:@"¥%.1f",price]];
        [self addSubview:_newsPriceLabel];
        
        
        //原价
       CGSize oldPriceSize =  [[NSString stringWithFormat:@"¥%.1f",prodDetail.prodOldPrice] sizeWithFont:SYS_FONT(PROD_OLDPRICE_FONT_SIZE)];
        _oldPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_newsPriceLabel.frame)+YSPublicXInset+5, CGRectGetMaxY(_prodImage.frame)-PROD_NEWPRICE_FONT_SIZE, oldPriceSize.width, oldPriceSize.height)];
        [_oldPriceLabel setTextColor:HEXColor(@"#999490")];
        [_oldPriceLabel setFont:[UIFont systemFontOfSize:PROD_OLDPRICE_FONT_SIZE]];
        NSString *oldPrice = [NSString stringWithFormat:@"¥%.1f",prodDetail.prodOldPrice];
        [UIUtil addMiddleLineWithLabel:self.oldPriceLabel text:oldPrice];
        [self addSubview:_oldPriceLabel];
        _oldPriceLabel.hidden = YES;
        
        
        //关闭按钮
        _closeBtn = [[UIButton alloc]init];
        UIImage *closeImg = [UIImage imageNamed:@"btn_close_normal"];
        [_closeBtn setBackgroundImage:closeImg forState:UIControlStateNormal
         ];
        [_closeBtn setFrame:CGRectMake(SCREENW-YSPublicXInset-closeImg.size.width, YSPublicXInset+5, closeImg.size.width, closeImg.size.height)];
        [_closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeBtn];
        
        _standardLabel = [[UILabel alloc]initWithFrame:CGRectMake(YSPublicXInset, CGRectGetMaxY(_prodImage.frame) + 33,SCREENW -YSPublicXInset*2, TITLE_FONT_SIZE)];
        [_standardLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
        [_standardLabel setText:@"规格："];
        [self addSubview:_standardLabel];
        
        _skuSelectView = [[SkuSelectView alloc]initWithProdSubArray:prodDetail.prodSub];
        _skuSelectView.x = 0;
        _skuSelectView.y = CGRectGetMaxY(_standardLabel.frame);
        [_skuSelectView setDelegate:self];
        [self addSubview:_skuSelectView];
        
        NSLog(@"_skuSelectView------%@",NSStringFromCGRect(_skuSelectView.frame));
        
        _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(YSPublicXInset, CGRectGetMaxY(_skuSelectView.frame) + 33,SCREENW -YSPublicXInset*2, TITLE_FONT_SIZE)];
        [_numLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
        [_numLabel setText:@"数量："];
        [self addSubview:_numLabel];
        
        _calcView = [[CalcView alloc]init];
        _calcView.x = 0;
        _calcView.y = CGRectGetMaxY(_numLabel.frame)+YSPublicXInset;
        [self addSubview:_calcView];
        
        //底部View
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_calcView.frame)+40, SCREENW, 49)];
        [_bottomView setBackgroundColor:[UIColor whiteColor]];
        //分割线
        UIView *view = [[UIView alloc]init];
        [view setFrame:CGRectMake(0, 0, _bottomView.width, 0.5)];
        [view setBackgroundColor:RGBColor(204, 204, 204)];
        [_bottomView addSubview:view];
        
        //购买按钮
        _buyBtn = [[UIButton alloc]initWithFrame:CGRectMake((_bottomView.width-203)/2, (49-35)/2, 203, 35)];
        [_buyBtn  setBackgroundImage:[UIImage imageWithColor:ThemeColor] forState:UIControlStateNormal];
        [_buyBtn setBackgroundImage:[UIImage imageWithColor:HEXColor(@"#b9b9b9")] forState:UIControlStateDisabled];
        [_buyBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:17]]];
        [_buyBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_buyBtn setTitle:@"已抢光" forState:UIControlStateDisabled];
        [_buyBtn setCornerRadius:5 borderWidth:0 borderColor:[UIColor clearColor]];
        [_buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_buyBtn setBackgroundImage:[UIImage imageNamed:@"bg_btn_gray"] forState:UIControlStateDisabled];
        [_bottomView addSubview:_buyBtn];
        
        [self addSubview:_bottomView];
        
        if ([prodDetail isCanBuy]==0) {
            [_buyBtn setEnabled:NO];
        }
        self.frame = CGRectMake(0, 0, SCREENW, CGRectGetMaxY(self.bottomView.frame));
    }
    return self;
}

-(void)buyBtnClick:(UIButton *)btn{
    if (![self hasOneChecked]) {
        ShowTips(@"请先选择一件商品");
        return;
    }
    if ([self.delegate respondsToSelector:@selector(buyBtnClickWtihCount:type:)]) {
        [self.delegate buyBtnClickWtihCount:[_calcView getNum] type:self.type];
        //选中的sku重新恢复为不选中
        [self clearSelected];
    }
    
}

-(void)skuBtnClickWithId:(NSInteger)tag isCanbuy:(BOOL)isCanbuy{
    [_buyBtn setEnabled:isCanbuy];
    
    YSSubProduct *prod = _result.prodSub[tag];
    CGSize newPriceSize =  [[NSString stringWithFormat:@"¥%.0f",prod.prodSubPrice] sizeWithFont:SYS_FONT(PROD_NEWPRICE_FONT_SIZE)];
    [_newsPriceLabel setWidth:newPriceSize.width];
    [_newsPriceLabel setText:[NSString stringWithFormat:@"¥%.0f",prod.prodSubPrice]];
    
    CGSize oldPriceSize =  [[NSString stringWithFormat:@"¥%.0f",prod.prodOldPrice] sizeWithFont:SYS_FONT(PROD_OLDPRICE_FONT_SIZE)];
    NSString *oldPrice = [NSString stringWithFormat:@"¥%.0f",prod.prodOldPrice];
    [UIUtil addMiddleLineWithLabel:self.oldPriceLabel text:oldPrice];
    [_oldPriceLabel setWidth:oldPriceSize.width];
    [_oldPriceLabel setX:CGRectGetMaxX(_newsPriceLabel.frame)+YSPublicXInset+5];
}

-(void)closeBtnClick:(UIButton *)btn{
    NSLog(@"closeBtnClick----%@",[self superview]);
    [self clearSelected];
    CustomShowView *showView =  (CustomShowView*)[self superview];
    [showView hideWithAnim:YES];
}

-(NSUInteger)getSelectIndex{
    return [_skuSelectView getSelectIndex];
}

-(BOOL)hasOneChecked{
    return [_skuSelectView hasOneChecked];
}

-(void)clearSelected{
    [_skuSelectView clearSelected];
}

@end
